package com.soonfor.evaluate.presenter;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import com.soonfor.evaluate.bean.ReplyDataBean;
import com.soonfor.evaluate.bean.ReturnVisitBean;
import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.activity.Evaluate_CustomersToMeDetailActivity;
import com.soonfor.evaluate.base.IEvalBasePresenter;
import com.soonfor.evaluate.bean.EvaluateListDetailBean;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 作者：DC-DingYG on 2018-10-18 11:29
 * 邮箱：dingyg012655@126.com
 */
public class Evaluate_CustomerToMeDetailPresenter implements IEvalBasePresenter, AsyncUtils.AsyncCallback {

    private Evaluate_CustomersToMeDetailActivity detailActivity;

    public Evaluate_CustomerToMeDetailPresenter(Evaluate_CustomersToMeDetailActivity detailActivity) {
        this.detailActivity = detailActivity;
    }

    /**
     *
     * @param isRefresh
     * @param sousetype 客户评价来源类型
     * @param souseId 客户评价来源id
     */
    public void getEvalDetailData(boolean isRefresh, int sousetype, String souseId) {
        //请求评价详情信息
        Request.Evaluate.getEvaluateDetailInfo(detailActivity.getBaseContext(), 0, sousetype, souseId,
                Request.Evaluate.GET_EVALUATETOMEDETAILINFO,this);
    }
    /**
     *
     * @param frtnvisitplantaskid 回访id
     */
    public void getRtnInfo(final String frtnvisitplantaskid){
        //请求人工回访结果
        Request.Evaluate.getReturnVisitResult(detailActivity.getBaseContext(), frtnvisitplantaskid, new AsyncUtils.AsyncCallback() {
            @Override
            public void success(int requestCode, String object) {
                Gson gson = new Gson();
                List<ReturnVisitBean> rvists = new ArrayList<>();
                ReplyDataBean replyDataBean = null;
                String reVisitData = null, replayData = null;
                try {
                    JSONObject jo = new JSONObject(object);
                    reVisitData = jo.getString("rtnvisitdata");//回访数据集
                    replayData = jo.getString("replydata");//回复
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if(reVisitData!=null && !reVisitData.equals("")){
                    try {
                        JSONArray ja = new JSONArray(reVisitData);
                        if (ja != null && ja.length() > 0) {
                            for (int i = 0; i < ja.length(); i++) {
                                String item = ja.getJSONObject(i).toString();
                                ReturnVisitBean visitBean = gson.fromJson(item, ReturnVisitBean.class);
                                if(visitBean.getFtype() == 1){
                                    List<ReturnVisitBean.Answer> answers = new ArrayList<>();
                                    ReturnVisitBean.Answer answer = new ReturnVisitBean.Answer("是", visitBean.getFpoint(), visitBean.getFynval());
                                    answers.add(answer);
                                    answer = new ReturnVisitBean.Answer("否", visitBean.getFpoint(), visitBean.getFynval()==0 ? 1:0);
                                    answers.add(answer);
                                    visitBean.setItem(answers);
                                }
                                rvists.add(visitBean);
                            }
                        }
                    }catch (Exception e){}
                }
                if(replayData!=null && !replayData.equals("")){
                    try {
                        replyDataBean = gson.fromJson(replayData, ReplyDataBean.class);
                    }catch (Exception e){}
                }
                detailActivity.setGetReturnVisit( true, rvists, replyDataBean);
            }
            @Override
            public void fail(int requestCode, int statusCode, String data, String msg) {
                detailActivity.setGetReturnVisit(false, null, null);
            }
        });
    }

    @Override
    public void success(int requestCode, String data) {
        final Gson gson = new Gson();
        switch (requestCode){
            case Request.Evaluate.GET_EVALUATETOMEDETAILINFO://获取详情
                EvaluateListDetailBean detailBean = gson.fromJson(data, EvaluateListDetailBean.class);
                detailActivity.setGetDetail(true, detailBean);
                break;
        }
    }
    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        switch (requestCode){
            case Request.Evaluate.GET_EVALUATETOMEDETAILINFO://获取详情
                detailActivity.setGetDetail(false, null);
                break;
        }
    }
}
